import sys, time, re, json
import sqlite3
import math, collections, itertools
import spade, nltk
from os import walk

path = "../datasets/hotel/"

conn = sqlite3.connect('Reviews_from_Crete.db')
hotels = conn.cursor()
reviews = conn.cursor()

for row in hotels.execute("SELECT * FROM hotels"):
	hotel = dict()
	hotel['hotel_id'] = row[0]
	hotel['hotel_name'] = row[1]
	hotel['stars'] = row[2]
	hotel['location_id'] = row[3]
	hotel['location_name'] = row[4]
	hotel['ratings'] = {'overall': row[5], 'clean': row[6], 'service': row[7], 'location': row[8], 'rooms': row[9], 'sleep_quality': row[10], 'value': row[11]}
	hotel['url'] = row[12]
	f = open(path + hotel['hotel_name'] + ".txt",'w')
	f.write(json.dumps(hotel))
	f.write('\n')
	for row in reviews.execute("SELECT * FROM reviews WHERE hotel_id=?", (hotel['hotel_id'],)):
		review = dict()
		review['review_id'] = row[0]
		review['title'] = row[2]
		review['text'] = row[3]
		review['ratings'] = {'overall': row[4], 'clean': row[5], 'service': row[6], 'location': row[7], 'rooms': row[8], 'sleep_quality': row[9], 'value': row[10]}
		review['url'] = row[11]
		f.write(json.dumps(review))
		f.write('\n')
	f.close()


path = "../datasets/hotel/"
for (_, _, filenames) in walk(path):
	for f in filenames[:1]:
		reviews = []
		w = open(path + f,'r')
		hotel = json.loads(w.readline())
		for line in w:
			reviews.append(json.loads(line))
		print reviews[0]


